
package ro.gateway.aida.struts.action;

import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.db.CategoryDB;
import ro.gateway.aida.struts.form.EditCategoryForm;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.Utils;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;


/**
 * EditCategoryAction.java created by EasyStruts - XsltGen.
 * http://easystruts.sf.net
 * created on 10-09-2003
 *
 * XDoclet definition:
 * @struts:action path="/categories" name="editCategoryForm" attribute="editCategoryForm" input="/orgType/edit.jsp" validate="true"
 */
public class EditCategoryAction extends TilesAction {


    /**
     * Method execute
     * @param ActionMapping mapping
     * @param ActionForm form
     * @param HttpServletRequest request
     * @param HttpServletResponse response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        EditCategoryForm ecf = (EditCategoryForm) form;
        ServletContext sc = getServlet().getServletContext();
		PersistenceToken token = PersistenceManager.
						tokenLookup(sc);
        CategoryDB cdb = CategoryDB.getManager(token);
      
		Arbiter ar = Arbiter.getManager(token);
		User user = UserUtils.getLogged(request, response);
		HttpSession session = request.getSession();
        LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request, sc);
        String lang = lang_bean.language;
        String mode = request.getParameter("mode");
      

		if (user==null || !ar.canDo(request, Perms.ADMIN_TOOLS)) {
					   session.invalidate();

					   return mapping.findForward("norights");
				   }

        if ("list".equals(mode)) {
            ArrayList categories = cdb.getAll(lang);
            request.setAttribute("categories", categories);
			Utils.setBreadCrumbs(request,"bc.categories.view");
            return mapping.findForward("viewcategories");
        }

        if ("delete".equals(mode)) {
            String[] delbm = request.getParameterValues("delcat");
			boolean delok=true;
            if (delbm != null)
                for (int i = 0; i < delbm.length; i++)
                    delok=delok && cdb.delete(Integer.parseInt(delbm[i]));
			if (!delok)
				return mapping.findForward("errorDelete");
			
			ArrayList categories = cdb.getAll(lang);
			request.setAttribute("categories", categories);
			Utils.setBreadCrumbs(request,"bc.categories.view");
            return mapping.findForward("viewcategories");
        }

        if (request.getParameter("id") != null ) {
           int id = Integer.parseInt(request.getParameter("id")); 
           

            if (request.getParameter("add_update") == null) {
                EditCategoryForm x = cdb.get(id, lang);
                ecf.setName(x.getName());
                ecf.setId(x.getId());
            }
        }

        if (request.getParameter("add_update") != null) {
            if (ecf.getId() == 0) {
                //add				
                ecf.setId(cdb.addLang(ecf, lang));
            }
            else
                //update
                cdb.updateLang(ecf, lang);
              
        }

     ArrayList categories = cdb.getAll(lang);
     request.setAttribute("categories", categories);
	 Utils.setBreadCrumbs(request,"bc.categories.edit");
        return mapping.getInputForward();
    }
}
